<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="2-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">{{ isEdit?'修改':'添加' }}</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
        <el-button size="small" @click="$refs.form.resetFields()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getDepartments } from '@/api/employees'
import { addDepartment, getDepartmentById, updateDepartmentById } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validName = (rule, value, cb) => {
      let nameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      if (this.isEdit) {
        // const pid = this.originList.find(item => item.id === this.id).pid
        // nameList = this.originList.filter(item => item.pid === pid && item.id !== this.id).map(item => item.name)
        nameList = this.originList.filter(item => item.pid === this.originList.find(item => item.id === this.id).pid && item.id !== this.id).map(item => item.name)
      }
      if (nameList.includes(value)) {
        cb(new Error(value + '已经存在'))
      } else {
        cb()
      }
    }
    const validCode = (rule, value, cb) => {
      let codeList = this.originList.map(item => item.code)
      if (this.isEdit) {
        codeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (codeList.includes(value)) {
        cb(new Error(value + '已经存在'))
      } else {
        cb()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '部门名称要求2-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  //   watch: {
  //     id: {
  //       handler(newVal, oldVal) {
  //         this.loadDepartment()
  //       },
  //       deep: true,
  //       immediate: true
  //     }
  //   },
  created() {
    this.loadDepartments()
    this.loadDepartment()
  },
  methods: {
    hSubmit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        try {
          let res = null
          if (!this.isEdit) {
            res = await addDepartment({ ...this.form, pid: this.id })
          } else {
            res = await updateDepartmentById(this.form)
          }
          this.$message.success(res.message)
          this.$emit('success')
        } catch (e) {
          this.$message.error(e.message)
        }
      })
    },
    hCancel() {
      this.$emit('close')
    },
    async loadDepartments() {
      const res = await getDepartments()
      this.employees = res.data
    },
    async loadDepartment() {
      if (!this.isEdit) return
      try {
        const res = await getDepartmentById(this.id)
        this.form = res.data
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>
